<?php
namespace User\Controller;

use Common\Controller\MemberbaseController;

class CenterController extends MemberbaseController {
	
	function _initialize(){
		parent::_initialize();
		$username = $this->user['user_login'];
		$userid = $this->user['id'];
		$expires = $this->user['expires'];
		$this->assign('username',$username);
		$this->assign('userid',$userid);
		$this->assign('user_ip',$this->user['last_login_ip']);
		$this->assign('exprie_time',date('Y-m-d H:i:s',$expires));
	}
	
    // 会员中心首页
	public function index() {
    	$this->display(':center');
    }

    //顶部
	public function top() {
		$this->display();
	}
	public function main1() {
		$this->display();
	}
	public function main() {
		$this->display();
	}

	public function menu() {
		$this->display();
	}

	//菜单
	public function report() {
		$model = M('report');
		$userid = sp_get_current_userid();
		$where['userid'] = $userid;
		//查询今日访问数量
		$start_time = strtotime(date('Y-m-d'));
		$end_time = strtotime(date('Y-m-d 23:59:59'));
		$where_time['add_time'] = array('between',array($start_time,$end_time));
        $where_time['userid'] = $userid;
		$access_num = $model->where($where_time)->count('DISTINCT ip');
		//只显示48小时
		$time = strtotime("- 48 hour");
		$where['add_time'] = array('gt',$time);
		$count = $model->where($where)->count();
		$page = $this->page($count);
		$lists = $model->where($where)->order('add_time desc')->limit($page->firstRow,$page->listRows)->select();
		$this->assign('lists',$lists);
		$this->assign('access_num',$access_num);
		$this->assign('page',$page->show());
		$this->display();
	}
	public function report_del(){
		$id = I('get.id');
		$where['id'] = $id;
		$userid = sp_get_current_userid();
		$where['userid'] = $userid;
		$del = M('report')->where($where)->delete();
		if($del){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}
	public function report_del_batch(){
		$ids = I('post.id');
		$where['id'] = array('in',$ids);
		$userid = sp_get_current_userid();
		$where['userid'] = $userid;
		$del = M('report')->where($where)->delete();
		if($del){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}
	public function ad() {
		$userid = sp_get_current_userid();
		$info = M('advert')->where("userid = $userid")->find();
		$info['domain'] = implode(',',json_decode($info['domain'],true));
		$info['kw'] = implode(',',unserialize($info['kw']));
		$this->assign('info',$info);
		$this->display();
	}
	public function ad_action(){
		if(IS_POST){
			$data = I('post.');
			$userid = sp_get_current_userid();
			$data['userid'] = $userid;
			$user_files = $_FILES;
			foreach ($user_files as $k => $v){
				if(!empty($user_files[$k]['tmp_name'])){
					$pic = $this->upload_ad_pic($user_files[$k]);
					//赋值入库
					$path = '../uploads/';
					$data[$k] = $path.  $pic['savepath'] . $pic['savename'];
				}
			}
			//保存多个域名，转换为数组 保持四个域名
			//保存多个关键词 最多10个
			$data['domain'] = json_encode(array_slice(explode(',',$data['domain']),0,4));
			$data['kw'] = serialize(array_slice(explode(',',$data['kw']),0,10));
			$model = M('advert');
			$where['userid'] = $userid;
			$res = $model->where($where)->find();
			if(!$res){
				$id = $model->add($data);
				if($id){
					$this->success('添加成功');
				}else{
					$this->error('添加失败，请联系管理员');
				}
			}else{
				$rlt = $model->where($where)->save($data);
				if($rlt !== false){
					$this->success('编辑成功');
				}else{
					$this->error('编辑失败，请联系管理员');
				}
			}

		}
	}
	public function download() {
		$site_url = sp_get_host();
		$userid = sp_get_current_userid();
		$this->assign('site_url',$site_url);
		$this->assign('userid',$userid);
		$this->display();
	}
	public function range() {
		$userid = sp_get_current_userid();
		$info = M('advert')->where("userid = $userid")->find();
		$info['engine'] = explode(',',$info['engine']);
		$this->assign('info',$info);
		$this->display();
	}
	public function range_post() {
		$post = I('post.');
		$data = $post;
		$data['engine'] = implode(',',$post['engine']);
		$userid = sp_get_current_userid();
		$model = M('advert');
		$where['userid'] = $userid;
		$res = $model->where($where)->find();
		if($res){
			$info = $model->where($where)->save($data);
			if($info !== false){
				$this->success('编辑成功');
			}else{
				$this->error('编辑失败，请联系管理员');
			}
		}else{
			$data['userid'] = $userid;
			$sertid = $model->add($data);
			if($sertid){
				$this->success('编辑成功');
			}else{
				$this->error('编辑失败，请联系管理员');
			}
		}
	}

	//上传图片
	public function upload_ad_pic($file){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath  =      './uploads/'; // 设置附件上传根目录
		$upload->savePath  =      ''; // 设置附件上传（子）目录
		$upload->autoSub  = true;
		$upload->subName  = array('date','Ymd');
        $upload->saveName = array('uniqid', mt_rand(1,999999).'_'.md5(uniqid()));//上传文件的保存规则
// 上传文件
		$info   =   $upload->uploadOne($file);
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
		}else{// 上传成功 获取上传文件信息
			return $info;
		}
	}


}
